package com.lanxiao.cloud.modules.dept.controller;

import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.dept.application.DeptCmdManager;
import com.lanxiao.cloud.modules.dept.application.DeptQueryManager;
import com.lanxiao.cloud.modules.dept.application.dto.DeptCmdAdd;
import com.lanxiao.cloud.modules.dept.application.dto.DeptCmdUpdate;
import com.lanxiao.cloud.modules.dept.application.dto.DeptTreeDto;
import com.lanxiao.cloud.modules.dept.application.dto.BasicsDeptTreeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-10-26
 */
@Api(tags = "部门")
@Validated
@RequestMapping("/depts")
@RestController
public class DeptController {

  private final DeptQueryManager deptQueryManager;
  private final DeptCmdManager deptCmdManager;

  public DeptController(
      DeptQueryManager deptQueryManager,
      DeptCmdManager deptCmdManager) {
    this.deptQueryManager = deptQueryManager;
    this.deptCmdManager = deptCmdManager;
  }

  @ApiOperation(value = "查询部门集合-结构树")
  @GetMapping("/tree")
  public List<DeptTreeDto> treeQuery() {
    return deptQueryManager.all();
  }

  @ApiOperation(value = "查询可分配部门集合-结构树")
  @GetMapping("/basics")
  public List<BasicsDeptTreeDto> query() {
    return deptQueryManager.query();
  }

  @ApiOperation(value = "新增部门")
  @Log(operationType = OperationType.CREATE)
  @PostMapping
  public ResponseEntity<Void> save(@Valid @RequestBody DeptCmdAdd add) {
    deptCmdManager.save(add);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @ApiOperation(value = "修改部门")
  @Log(operationType = OperationType.UPDATE,
      serviceClass = DeptQueryManager.class)
  @PutMapping("/{id}")
  public ResponseEntity<Void> update(@PathVariable Long id,
      @Valid @RequestBody DeptCmdUpdate update) {
    deptCmdManager.update(id, update);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

}
